package org.jeecg.modules.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.common.bean.mqtt.*;
import org.jeecg.modules.common.entity.CamelliaDevice;
import org.jeecg.modules.common.service.ICamelliaDeviceService;
import org.jeecg.modules.common.service.MqttGateway;
import org.jeecg.modules.common.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags="MQTT服务类")
@RestController
@RequestMapping(value = "/common/mqtt")
public class MqttController {

    @Autowired
    private MqttGateway mqttGateway;
    @Autowired
    private MqttService mqttService;

    @Autowired
    private ICamelliaDeviceService camelliaDeviceService;

    @ApiOperation(value="MQTT-发送消息", notes="MQTT-发送消息")
    @GetMapping(value = "/sendMessage")
    public Result<String> sendMessage(String topic, String message){
        mqttGateway.sendToMqtt(topic,1,message);
        return Result.ok("发送成功");
    }

    @ApiOperation(value="MQTT-订阅消息", notes="MQTT-订阅消息")
    @GetMapping(value = "/addSubscribeTopic")
    public Result<String> addSubscribeTopic(String subscribeTopic){
        mqttService.addSubscribeTopic(subscribeTopic);
        return Result.ok("订阅成功");
    }

    @ApiOperation(value="MQTT-移除订阅", notes="MQTT-移除订阅")
    @GetMapping(value = "/removeSubscribeTopic")
    public Result<String> removeSubscribeTopic(String subscribeTopic){
        mqttService.removeSubscribeTopic(subscribeTopic);
        return Result.ok("移除订阅成功");
    }
    @ApiOperation(value="MQTT-重启设备", notes="MQTT-重启设备")
    @GetMapping(value = "/reboot")
    public Result<String> reboot(String devicePid, Integer qos) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.reboot("/"+devicePid+"/sys/set",qos);
                return Result.ok("重启设备成功");
            }

        }
        return Result.error("未找到该设备");
    }

    @ApiOperation(value="MQTT-设置订阅周期", notes="MQTT-设置订阅周期")
    @GetMapping(value = "/setCyc")
    public Result<String> setCyc(String devicePid, Integer qos, MqttSysBean mqttSettingBean) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.setCyc("/"+devicePid+"/sys/set",qos,mqttSettingBean);
                return Result.ok("设置订阅周期成功");
            }
        }
        return Result.error("未找到该设备");

    }

    @ApiOperation(value="MQTT-设置阈值", notes="MQTT-设置阈值")
    @GetMapping(value = "/setThv")
    public Result<String> setThv(String devicePid, Integer qos, MqttThresholdBean mqttThresholdBean) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.setThv("/"+devicePid+"/sys/set", qos, mqttThresholdBean);
                return Result.ok("设置阈值成功");
            }
        }
        return Result.error("未找到该设备");
    }

    @ApiOperation(value="MQTT-设置模拟曲线", notes="MQTT-设置模拟曲线")
    @GetMapping(value = "/setAI")
    public Result<String> setAI(String devicePid, Integer qos, MqttAIBean mqttAIBean) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.setAI("/"+devicePid+"/sys/set", qos, mqttAIBean);
                return Result.ok("设置AI曲线成功");
            }
        }
        return Result.error("未找到该设备");
    }

    @ApiOperation(value="MQTT-设置数字曲线", notes="MQTT-设置数字曲线")
    @GetMapping(value = "/setDI")
    public Result<String> setDI(String devicePid, Integer qos, MqttDiBean mqttDiBean) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.setDI("/"+devicePid+"/sys/set", qos, mqttDiBean);
                return Result.ok("设置DI曲线成功");
            }
        }
        return Result.error("未找到该设备");
    }

    @ApiOperation(value="MQTT-下发控制指令", notes="MQTT-下发控制指令")
    @GetMapping(value = "/setCMD")
    public Result<String> setCMD(String devicePid, Integer qos) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.setCMD("/"+devicePid+"/sys/set", qos, "getpara");
                return Result.ok("下发指令成功");
            }
        }
        return Result.error("未找到该设备");
    }

    @ApiOperation(value="MQTT-安全联动输入控制请求", notes="MQTT-安全联动输入控制请求")
    @GetMapping(value = "/control")
    public Result<String> control(String devicePid, Integer qos,MqttControlBean mqttControlBean) {
        List<CamelliaDevice> list = camelliaDeviceService.list();
        for (CamelliaDevice camelliaDevice : list) {
            if (camelliaDevice.getDevicePid().equals(devicePid)){
                mqttService.control("/"+devicePid+"/control/request",qos,mqttControlBean);
                return Result.ok("下发指令成功");
            }
        }
        return Result.error("未找到该设备");
    }




}

